"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 数据库系统
Case Name   : 数据库备机控制文件权限丢失，数据库重启
Create At   : 2020/07/20
@zou_jialiang0505328114
Description :
    1.启动主备数据库，判断数据库状态；
    2.修改备机数据库控制文件权限；
    3.进行数据交易
    4.重启主备
    5.恢复备机数据库控制文件权限
    6.重启主备
Expect      :
    1.启动主备数据库，判断数据库状态正常；
    2.修改备机数据库控制文件权限成功
    3.进行数据交易成功
    4.重启主备失败
    5.恢复备机数据库控制文件权限成功
    6.重启主备成功
History     :
    modified：2021-3-5 by 5328113;修改备机control文件权限后，备机未stop，修改断言及整个用例逻辑
    modified：2021-3-10 by 5328113;文件类故障删除断言，只保证恢复后功能正常即可
    Modified by @li-xin12345 2023/2/7:增加插入数据量
"""
import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Deletaduit(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('----Opengauss_Reliability_Dbsys_Case147.py start----')
        self.com = Common()
        self.sta1_root = Node(node='Standby1Root')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.sta_1_sh = CommonSH('Standby1DbUser')
        self.Constant = Constant()
        self.control_file = os.path.join(macro.DB_INSTANCE_PATH,
                                         'global',
                                         'pg_control')
        self.MAX_NUM = 100000
        self.table_test = 't_Dbsys_Case147'
        self.insert_sql = f'''drop table if exists {self.table_test};
            create table {self.table_test}(id int);
            begin 
            for i in 1..{self.MAX_NUM} loop
            insert into {self.table_test} values(i);
            end loop;
            end;'''
        self.select_sql = f'select count(*) from {self.table_test};'
        self.drop_sql = f'drop table {self.table_test};'
        self.node_num = self.com.get_node_num(self.sta1_root)
        self.log.info(self.node_num)
        self.min_time = 300

    def test_startdb(self):
        try:
            self.log.info('----修改备机数据库控制文件权限----')
            chmod_cmd = f'chmod 000 {self.control_file}'
            self.log.info(chmod_cmd)
            self.sta1_root.sh(chmod_cmd)
            ll_cmd = f'ls -al {self.control_file}'
            self.log.info(ll_cmd)
            self.log.info(self.sta1_root.sh(ll_cmd).result())

            self.log.info('----创建数据表，并插入数据功能正常----')
            insert_result = self.pri_sh.executDbSql(self.insert_sql)
            self.log.info(insert_result)
            select_result = self.pri_sh.executDbSql(self.select_sql)
            self.log.info(select_result)
            self.assertIn(str(self.MAX_NUM), select_result)

            self.log.info('----查询主备状态----')
            status = self.pri_sh.getDbClusterStatus('status')
            self.assertFalse(status)

            self.log.info('----重启主备----')
            self.pri_sh.stopDbCluster()
            time.sleep(2)
            self.pri_sh.startDbCluster(True)

        finally:
            self.log.info('----恢复备机数据库控制文件权限----')
            ll_cmd = f'ls -al {self.control_file}'
            self.log.info(ll_cmd)
            self.log.info(self.sta1_root.sh(ll_cmd).result())
            chmod_cmd = f'chmod 600 {self.control_file}'
            self.log.info(chmod_cmd)
            self.log.info(self.sta1_root.sh(chmod_cmd).result())
            ll_cmd = f'ls -al {self.control_file}'
            self.log.info(ll_cmd)
            self.log.info(self.sta1_root.sh(ll_cmd).result())

            self.log.info('----重启主备----')
            is_stop = self.pri_sh.stopDbCluster()
            self.assertTrue(is_stop)
            time.sleep(2)
            is_start = self.pri_sh.startDbCluster()
            self.assertTrue(is_start)

            self.log.info('----查看主机query，同步是否正常----')
            self.pri_sh.check_location_consistency('primary',
                                                   self.node_num,
                                                   self.min_time)

            self.log.info('----备机查询数据插入情况----')
            select_result = self.sta_1_sh.executDbSql(self.select_sql)
            self.log.info(select_result)
            self.assertIn(str(self.MAX_NUM), select_result)

    def tearDown(self):
        self.log.info('----清理测试表----')
        drop_result = self.pri_sh.executDbSql(self.drop_sql)
        self.log.info(drop_result)

        self.log.info('----Opengauss_Reliability_Dbsys_Case147.py finish----')
